package org.wevil.aspect;

import org.wevil.entity.log.LogController;
import org.wevil.entity.log.LogLogin;
import org.wevil.params.login.LoginNameParam;
import org.wevil.params.login.LoginTelParam;
import org.wevil.props.ProjectProp;
import org.wevil.service.sys.LogService;
import org.wevil.vo.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 日志切面
 */
@Aspect
@Component
public class LogAspect {

    @Resource
    LogService logService;
    @Resource
    ProjectProp projectProp;

//    @Around(value = "@annotation(base.aspect.LogAnnotation)")
    @Around(value = "execution(* org.wevil.controller.*.*(..))")
    public Object controllerLog(ProceedingJoinPoint joinPoint) throws Throwable {
        LogController log = new LogController();
        String reqClass = "";
        String reqMethod = "";
        Integer resCode = 0;
        String reqIp;
        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        reqIp = request.getRemoteAddr();
        Object[] args = joinPoint.getArgs();
        if(args != null){
            // 获取当前执行类名
            reqClass = joinPoint.getSignature().getDeclaringTypeName();
            reqClass = reqClass.replace(projectProp.getGroupId() + ".controller.", "");
            // 获取当前执行的方法名
            reqMethod = joinPoint.getSignature().getName();
        }
        // 执行方法，返回执行结果
        Object object =  joinPoint.proceed();
        if(object instanceof Result){
            Result<?> r = (Result<?>) object;
            resCode = r.getCode();
        }
        log.setReqClass(reqClass);
        log.setReqMethod(reqMethod);
        log.setResCode(resCode);
        log.setReqIp(reqIp);
        logService.saveControllerLog(log);
        return object;
    }

    @Around(value = "@annotation(org.wevil.aspect.LogAnnotation)")
    public Object loginLog(ProceedingJoinPoint joinPoint) throws Throwable {
        LogLogin log = new LogLogin();
        String username = "";
        String tel = "";
        String password = "";
        String reqIp;
        Integer resCode = 0;
        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        reqIp = request.getRemoteAddr();
        Object[] args = joinPoint.getArgs();
        if(args != null){
            Object obj = args[0];
            if(obj instanceof LoginTelParam){
                LoginTelParam param = (LoginTelParam)obj;
                tel = param.getTel();
                password = param.getPassword();
            }
            if(obj instanceof LoginNameParam){
                LoginNameParam param = (LoginNameParam)obj;
                username = param.getUsername();
                password = param.getPassword();
            }
        }
        // 执行方法，返回执行结果
        Object object =  joinPoint.proceed();
        if(object instanceof Result){
            Result<?> r = (Result<?>) object;
            resCode = r.getCode();
        }
        log.setUsername(username);
        log.setTel(tel);
        log.setPassword(password);
        log.setReqIp(reqIp);
        log.setResCode(resCode);
        logService.saveLoginLog(log);
        return object;
    }
}
